package com.likou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class q17 {

    List<String> ans = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.isEmpty()) {
            return ans;
        }
        Map<Character, String> map = new HashMap<>();
        map.put('2', "abc");
        map.put('3', "def");
        map.put('4', "ghi");
        map.put('5', "jkl");
        map.put('6', "mno");
        map.put('7', "pqrs");
        map.put('8', "tuv");
        map.put('9', "wxyz");

        StringBuilder stringBuilder = new StringBuilder();
        DFS(ans, map, 0, stringBuilder, digits);

        return ans;
    }

    private void DFS(List<String> ans, Map<Character, String> map, int index, StringBuilder stringBuilder, String digits) {
        if (index == digits.length()) {
            ans.add(String.valueOf(stringBuilder));
            return;
        }
        char charAt = digits.charAt(index);
        String s = map.get(charAt);

        for (int i = 0; i < s.length(); i++) {
            stringBuilder.append(s.charAt(i));
            DFS(ans, map, index + 1, stringBuilder, digits);
            stringBuilder.deleteCharAt(index);
        }

    }
}
